package org.PropertyManageMentSystem.Servive;
import org.PropertyManageMentSystem.Data.*;
import org.PropertyManageMentSystem.Manager.*;

import javax.management.relation.Role;
import java.text.ParseException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

public class DirectorService {

    private Director director;

    private MysqlConfig mysqlConfig;
    private ScheduleManager ScheduleManager ;
    private ActionManager ActionManager;

    private ComplaintManager ComplaintManager;

    private ComplaintReplyManager complaintReplyManager;

    private TaskManager taskManager;
    private CommentManager commentManager;

    private WorkerManager workerManager;

    private SchedulerManager schedulerManager;

    public DirectorService(Long directorId, MysqlConfig mysqlConfig) throws SQLException, ClassNotFoundException {

        this.mysqlConfig=mysqlConfig;
        ScheduleManager = new ScheduleManager(mysqlConfig);
        ActionManager= new ActionManager(mysqlConfig);
        ComplaintManager= new ComplaintManager(mysqlConfig);
        taskManager = new TaskManager(mysqlConfig);
        commentManager = new CommentManager(mysqlConfig);
        workerManager = new WorkerManager(mysqlConfig);
        schedulerManager = new SchedulerManager(mysqlConfig);
        complaintReplyManager = new ComplaintReplyManager(mysqlConfig);
    }

    public Integer getCost(Long taskId) throws SQLException {

        ArrayList<Schedule> schedules;
        long Time = 0;
        schedules = ScheduleManager.getScheduleByTaskId(taskId);

        for (Schedule Schedule:schedules) {
            ArrayList<Action> Actions = ActionManager.getActionByScheduleId(Schedule.getScheduleId());
            for (Action Action:Actions) {
                Time += Action.getEndtime().getTime()-Action.getStarttime().getTime();   //结束时间-开始时间得到的时间戳累加
            }
        }
        int hour=(int)(Time/(60*60*1000));

        return hour;
    }
    public Long createComplaint(Long taskId, String info) throws Exception {

        Long complaintId = ComplaintManager.addComplaint(taskId,info);

        ArrayList<Schedule> scheduleArrayList = ScheduleManager.getScheduleByTaskId(taskId);
        ArrayList<Worker> workers = new ArrayList<>();
        ArrayList<Scheduler> schedulers = new ArrayList<>();

        for (Schedule schedule : scheduleArrayList){
            Long workerId = schedule.getWorkerId();
            Long schedulerId = schedule.getSchedulerId();
            Worker worker = workerManager.getWorkerById(workerId);
            Scheduler scheduler = schedulerManager.getSchedulerById(schedulerId);
            workers.add(worker);
            schedulers.add(scheduler);
        }

        // add empty replies
        for(Scheduler scheduler : schedulers){
            ComplaintReply.Role schedulerrole = ComplaintReply.Role.scheduler;
            complaintReplyManager.addEmptyComplaintReply(complaintId, schedulerrole, scheduler.getId());
        }

        for(Worker worker : workers){
            ComplaintReply.Role workerrole = ComplaintReply.Role.worker;
            complaintReplyManager.addEmptyComplaintReply(complaintId, workerrole, worker.getWorkerId());
        }

        return complaintId;
    }
    public ArrayList<Complaint> getAllComplaint(){
        return ComplaintManager.getAllComplaint();
    }
    public void finishComplaint(Long complaintId){
        ComplaintManager.finishComplaint(complaintId);
    }
    public void showPerformance(ComplaintReply.Role role, Long id) throws Exception {

        ArrayList<Schedule> schedules;
        long Time = 0;
        schedules = ScheduleManager.getScheduleByWorkerId(id);

        for (Schedule Schedule:schedules) {
            ArrayList<Action> Actions = ActionManager.getActionByScheduleId(Schedule.getScheduleId());
            for (Action Action:Actions) {
                Time += Action.getEndtime().getTime()-Action.getStarttime().getTime();   //结束时间-开始时间得到的时间戳累加
            }
        }
        int hour=(int)(Time/(60*60*1000));
        Worker worker = workerManager.getWorkerById(id);


        ArrayList<Long> taskIds = new ArrayList<Long>();
        for (Schedule Schedule:schedules) {
            if(!taskIds.contains(Schedule.getTaskId())){
                taskIds.add(Schedule.getTaskId());
            }
        };
        int timelinessScore=0;
        int attitudeScore=0;
        int satisfiedScore=0;

        for (Long taskId:taskIds) {
            ArrayList<Comment> comments= commentManager.getCommentByTaskId(taskId);
            for (Comment comment:comments) {
                timelinessScore += comment.getTimelinessScore();
                attitudeScore += comment.getAttitudeScore();
                satisfiedScore += comment.getSatisfiedScore();
            }
        }

        timelinessScore /=  taskIds.size();
        attitudeScore /=  taskIds.size();
        satisfiedScore /=  taskIds.size();
        System.out.println(worker.getName()+"的绩效为：时间"+timelinessScore+"分，态度"+attitudeScore
                            +"分，满意度"+satisfiedScore+"分，工作时间"+hour+"小时。");//统计个人工时 + 任务表现。

    }
    public ArrayList<Task> getAllAccomplishTask() throws SQLException {
        return taskManager.getTaskByState(Task.TaskStates.accomplished);
    }

    public ArrayList<Worker> getAllWorker() throws SQLException {
        return workerManager.getAllWorker();
    }

    public void createComment(Long taskId,int timelinessScore, int attitudeScore, int satisfiedScore, String info){
        commentManager.addComment(taskId,timelinessScore,attitudeScore, satisfiedScore, info);
    }
    public ArrayList<ComplaintReply> getComplaintReplyByComplaintId(Long complaintId){
        return complaintReplyManager.getComplaintReplyByComplaintId(complaintId);
    }

}
